package com.coinworld.service.impl;


import com.coinworld.entity.User;
import com.coinworld.object.HttpJsonResult;
import com.coinworld.redis.RedisPool;
import com.coinworld.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class UserServiceImpl implements UserService {
    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);


    @Resource
    RedisPool redisPool;

    @Resource
    private UserService userService;



    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public HttpJsonResult<User> login(String mobile, String shareId, String verifyCode) {
        HttpJsonResult<User> httpJsonResult = new HttpJsonResult<User>();
//        String code = redisPool.get(mobile, 2);
////        String code = "123456";
//        if (!verifyCode.equals(code)) {
//            httpJsonResult.setSuccess(false);
//            httpJsonResult.setMsg("短信验证码输入有误！");
//            return httpJsonResult;
//        }
//
//        String encryptedMobile = MD5Util.md5(MD5Util.md5(mobile).concat("FENGMILVXING"));
//        Integer userStatus = userDao.getUserStatus(encryptedMobile);
//        if (userStatus != null) {
//            httpJsonResult.setSuccess(false);
//            httpJsonResult.setMsg("已注册，去登录!");
//            return httpJsonResult;
//        }
//        User parent = userDao.queryUserByAccountId(shareId);
//        if (parent == null) {
//            httpJsonResult.setSuccess(false);
//            httpJsonResult.setMsg("分享码错误！");
//            logger.info("分享码错误！");
//            return httpJsonResult;
//        }
//        //判断用户邀请人数，大于标准禁用转账功能
//        if (1 == parent.getTransferAccountsStatus()) {
//            Integer fristToDay = userDao.getFristToDay(parent.getId());
//            if (fristToDay > 100) {
//                User editUser = new User();
//                editUser.setId(parent.getId());
//                editUser.setTransferAccountsStatus(0);
//                Integer integer = userDao.updateSelect(editUser);
//                if (integer > 0) {
//                    logger.info("用户:" + parent.getId() + "邀请人数大于标准,禁用转账功能！");
//                }
//            }
//        }
        try {
//            User user = new User();
//            user.setMobile(mobile);
//            user.setEncryptedMobile(encryptedMobile);
//            user.setCreateTime(new Date());
//            user.setNickName(mobile.substring(mobile.length() - 4));
//            user.setTeamLevel(0);
//            if (!"".equals(parent.getId())) {
//                user.setParentFirst(parent.getId());
//            }
//            if (!"".equals(parent.getParentFirst())) {
//                user.setParentSecond(parent.getParentFirst());
//            }
//            user.setAccountId(ShareCodeUtil.generateShortUuid());
//            int counts = userDao.init(user);
//            if (counts > 0) {
//                //注册成功后 初始化 userWallet
//                // User userId = userDao.getByMobile(encryptedMobile);
//                UserWallet userWallet = new UserWallet();
//                userWallet.setUserId(user.getId());
//                userWalletDao.insert(userWallet);
//                //根据分享码，设置上下级。
//                this.updateFirstAndSecond(user);
//                user.setMobile(encryptedMobile);
//                user.setId(null);
//                httpJsonResult.setData(user);
//                return httpJsonResult;
//            } else {
//                //注册失败
//                httpJsonResult.setSuccess(false);
//            }
        } catch (Exception ex) {
            httpJsonResult.setSuccess(false);
            httpJsonResult.setMsg("注册失败，请稍后再试！");
            logger.error("注册异常，异常信息:{}", ex);
            throw new RuntimeException("注册异常，异常信息:", ex);
        }
        return httpJsonResult;
    }

}
